package com.xinzhidi.web.house.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import com.xinzhidi.common.annotation.Log;
import com.xinzhidi.common.core.controller.BaseController;
import com.xinzhidi.common.core.domain.AjaxResult;
import com.xinzhidi.common.core.domain.R;
import com.xinzhidi.common.core.page.TableDataInfo;
import com.xinzhidi.common.enums.BusinessType;
import com.xinzhidi.common.utils.poi.ExcelUtil;
import com.xinzhidi.web.equipment.domain.dto.SetGroupByIdDto;
import com.xinzhidi.web.house.domain.Household;
import com.xinzhidi.web.house.domain.dto.HouseholdAddDto;
import com.xinzhidi.web.house.domain.dto.HouseholdListDto;
import com.xinzhidi.web.house.domain.dto.HouseholdUpdDto;
import com.xinzhidi.web.house.domain.vo.HouseholdExcelVo;
import com.xinzhidi.web.house.domain.vo.HouseholdImportVo;
import com.xinzhidi.web.house.domain.vo.HouseholdInfoVo;
import com.xinzhidi.web.house.domain.vo.HouseholdListVo;
import com.xinzhidi.web.house.service.IHouseholdService;
import com.xinzhidi.web.plot.domain.vo.SelectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 住户Controller
 *
 * @author cx
 * @date 2023-05-23
 */
@RestController
@Api(tags = "住户")
@RequestMapping("/household")
public class HouseholdController extends BaseController {
    @Autowired
    private IHouseholdService householdService;

    @ApiOperation("查询住户列表")
    @PreAuthorize("@ss.hasPermi('house:household:list')")
    @PostMapping("/listHousehold")
    public TableDataInfo<HouseholdListVo> listHousehold(@Validated @RequestBody HouseholdListDto dto) {
        startPagePost(dto);
        return householdService.selectHouseholdList(dto);
    }

    @ApiOperation("查询住户下拉列表")
    @PostMapping("/listHouseholdSelect")
    public TableDataInfo<SelectVo> listHouseholdSelect(@Validated @RequestBody HouseholdListDto dto) {
        dto.setPageSize(9999);
        startPagePost(dto);
        return householdService.listHouseholdSelect(dto);
    }

    @ApiOperation("导出住户列表")
    @PreAuthorize("@ss.hasPermi('house:household:export')")
    @Log(title = "住户", businessType = BusinessType.EXPORT)
    @PostMapping("/exportHousehold")
    public void exportHousehold(HttpServletResponse response, HouseholdListDto dto) {
        TableDataInfo<HouseholdListVo> list = householdService.selectHouseholdList(dto);
        ExcelUtil<HouseholdListVo> util = new ExcelUtil<HouseholdListVo>(HouseholdListVo.class);
        util.exportExcel(response, list.getRows(), "住户数据");
    }

    @Log(title = "导入住户", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('house:household:import')")
    @ApiOperation("导入住户")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<HouseholdImportVo> util = new ExcelUtil<HouseholdImportVo>(HouseholdImportVo.class);
        List<HouseholdImportVo> buildList = util.importExcel(file.getInputStream());
        String message = householdService.importBuild(buildList, updateSupport);
        return success(message);
    }

    @PostMapping("/importTemplate")
    @ApiOperation("模板导出")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<HouseholdImportVo> util = new ExcelUtil<HouseholdImportVo>(HouseholdImportVo.class);
        util.importTemplateExcel(response, "住户数据");
    }

    @ApiOperation("获取住户详细信息")
    @PreAuthorize("@ss.hasPermi('house:household:query')")
    @GetMapping(value = "/getHouseholdById")
    public R<HouseholdInfoVo> getHouseholdById(@RequestParam("id") Long id) {
        return R.ok(householdService.selectHouseholdById(id));
    }

    @ApiOperation("新增住户")
    @PreAuthorize("@ss.hasPermi('house:household:add')")
    @Log(title = "住户", businessType = BusinessType.INSERT)
    @PostMapping("addHousehold")
    public AjaxResult addHousehold(@Validated @RequestBody HouseholdAddDto dto) {
        return toAjax(householdService.insertHousehold(dto));
    }

    @ApiOperation("修改住户")
    @PreAuthorize("@ss.hasPermi('house:household:edit')")
    @Log(title = "住户", businessType = BusinessType.UPDATE)
    @PostMapping("editHousehold")
    public AjaxResult editHousehold(@Validated @RequestBody HouseholdUpdDto dto) {
        return toAjax(householdService.updateHousehold(dto));
    }

    @ApiOperation("删除住户")
    @PreAuthorize("@ss.hasPermi('house:household:remove')")
    @Log(title = "住户", businessType = BusinessType.DELETE)
    @DeleteMapping("/delHousehold")
    public AjaxResult delHousehold(@RequestParam Long[] ids) {
        return toAjax(householdService.deleteHouseholdByIds(ids));
    }


    @ApiOperation("批量分组")
    @PostMapping("setGroupByIds")
    public R setGroupByIds(@Validated @RequestBody SetGroupByIdDto dto) {
        Set<Long> collect = householdService.list(new LambdaQueryWrapper<>(Household.class).in(Household::getId, dto.getIds())).stream().map(Household::getPlotId).collect(Collectors.toSet());
        if (CollectionUtils.isEmpty(collect) || collect.size() > 1) {
            return R.fail("只能对同一小区数据进行批量分组");
        }
        householdService.update(new LambdaUpdateWrapper<>(Household.class).set(Household::getType, dto.getEquipmentGroupId()).in(Household::getId, dto.getIds()));
        return R.ok();
    }
}
